package com.geek.javase.ch17.videodemo.udp;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.Socket;
import java.net.SocketException;

/*
 *@Date 2025/11/6
 *@Author wangpeng
 *@Description 接收端
 */
public class UDPReceive {
    public static void main(String[] args) {
        byte[] bytes = new byte[1024];
        try {
            DatagramSocket ds = new DatagramSocket(8899);
            //DatagramPacket(byte[] buf, int length) 打包数据
            DatagramPacket dp = new DatagramPacket(bytes, bytes.length);
            //DatagramSocket receive(DatagramPacket p) 接收数据包
            ds.receive(dp);
            //dp.getData() 接收到的实际数据    dp.getLength() 接收到实际数据的长度
            System.out.println(new String(dp.getData(), 0, dp.getLength()));
            //关闭操作
            ds.close();
        } catch (SocketException e) {
            e.printStackTrace();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
